package export.strength
{
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.data.vo.PlayerDataVO;
	import com.game.manager.EventManager;
	import com.game.manager.GameSceneManager;
	import com.game.module.backpack.view.BackPackElement;
	import com.game.module.backpack.vo.BackPackEvent;
	
	import config.Config;
	
	import flash.display.*;
	import flash.events.*;
	import flash.text.TextField;
	
	public class StrengthEquipment extends Sprite
	{
		
		public function StrengthEquipment()
		{
			super();
			TextUtil.redrawTxtOld(this, ["txtlh", "nowpage"]);
			this.gc = Config.getIns();
			this.addEventListener(Event.ADDED_TO_STAGE, this.added, false, 0, true);
			this.addEventListener(Event.REMOVED_FROM_STAGE, this.removed, false, 0, true);
		}
		
		public var nowpage:TextField;
		
		public var txtlh:TextField;
		public var state:String = "";
		public var btnback:SimpleButton;
		public var strengthbtn:SimpleButton;
		public var mixturebtn:SimpleButton;
		public var makingbtn:SimpleButton;
		public var resolutionbtn:SimpleButton;
		public var prePage:SimpleButton;
		public var nextPage:SimpleButton;
		private var gc:Config;
		private var player1:MovieClip;
		private var player2:MovieClip;
		private var currentPlayer:PlayerDataVO;
		private var backpackagemc:BackPackElement;
		private var isfirst:Boolean = true;
		private var tag:Boolean = true;
		private var btnState:*;
		private var lastBtn:String = "";
		
		public function getUser():PlayerDataVO
		{
			return this.currentPlayer;
		}
		
		public function setTxtlh():void
		{
			if (this.currentPlayer)
			{
				this.txtlh.text = this.currentPlayer.lhValue + "";
			}
			else
			{
				trace("set lhValue text: error reason:'this.currentPlayer is null'");
			}
		}
		
		private function addBackPackAgeMc():void
		{
			if (this.backpackagemc != null && contains(this.backpackagemc))
			{
				this.removeChild(this.backpackagemc);
				this.backpackagemc = null;
			}
			this.backpackagemc = new BackPackElement();
			this.isfirst = true;
			this.addChild(this.backpackagemc);
			this.backpackagemc.setPlayer(this.currentPlayer);
			this.backpackagemc.x = 512.8;
			this.backpackagemc.y = 130;
		}
		
		private function onWho(param1:String):PlayerDataVO
		{
			var _loc2_:uint = this.gc.playNum;
			while (_loc2_ > 0)
			{
				if (this.gc["player" + _loc2_].getRoleName() == param1)
				{
					return this.gc["player" + _loc2_] as PlayerDataVO;
				}
				_loc2_--;
			}
			return null;
		}
		
		private function removeEquipCont():void
		{
			var _loc1_:* = this.getChildByName("playerControl");
			if (_loc1_ != null)
			{
				this.removeChild(_loc1_);
				_loc1_ = null;
			}
			if (!this.isfirst)
			{
				EventManager.getIns().dispatchEvent(BackPackEvent.REFRESH_BACKPACK);
			}
			this.isfirst = false;
		}
		
		private function back(param1:*):void
		{
			GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MAP);
			if (this.parent)
			{
				this.parent.removeChild(this);
			}
		}
		
		private function added(param1:Event):void
		{
			var _loc2_:* = 0;
			if (this.tag)
			{
				this.tag = false;
				this.player1 = null;
				this.player2 = null;
				_loc2_ = uint(this.gc.playNum);
				while (_loc2_ > 0)
				{
					if (this.gc["player" + _loc2_].getRoleName() == "孙悟空")
					{
						this["player" + _loc2_] = ResUtil.getNewObj("export.shop.SelectWK") as MovieClip;
						this["player" + _loc2_].name = "孙悟空";
					}
					else if (this.gc["player" + _loc2_].getRoleName() == "唐僧")
					{
						this["player" + _loc2_] = ResUtil.getNewObj("export.shop.SelectTS") as MovieClip;
						this["player" + _loc2_].name = "唐僧";
					}
					else if (this.gc["player" + _loc2_].getRoleName() == "八戒")
					{
						this["player" + _loc2_] = ResUtil.getNewObj("export.shop.SelectBJ") as MovieClip;
						this["player" + _loc2_].name = "八戒";
					}
					else if (this.gc["player" + _loc2_].getRoleName() == "沙僧")
					{
						this["player" + _loc2_] = ResUtil.getNewObj("export.shop.SelectSS") as MovieClip;
						this["player" + _loc2_].name = "沙僧";
					}
					this["player" + _loc2_].gotoAndStop(1);
					this["player" + _loc2_].buttonMode = true;
					this["player" + _loc2_].addEventListener(MouseEvent.CLICK, this.equipStrength);
					this.addChild(this["player" + _loc2_]);
					this["player" + _loc2_].x = 42 + (_loc2_ - 1) * 90;
					this["player" + _loc2_].y = 14.85;
					_loc2_--;
				}
				this.btnback.addEventListener(MouseEvent.CLICK, this.back);
				this.strengthbtn.addEventListener(MouseEvent.CLICK, this.strMethod);
				this.mixturebtn.addEventListener(MouseEvent.CLICK, this.mixMethod);
				this.resolutionbtn.addEventListener(MouseEvent.CLICK, this.resMethod);
				this.makingbtn.addEventListener(MouseEvent.CLICK, this.makeMethod);
				this.prePage.addEventListener(MouseEvent.CLICK, this.prePageClick);
				this.nextPage.addEventListener(MouseEvent.CLICK, this.nextPageClick);
				this.player1.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
		}
		
		private function removed(param1:Event):void
		{
			if (this.player1)
			{
				this.player1.removeEventListener(MouseEvent.CLICK, this.equipStrength);
			}
			if (this.player2)
			{
				this.player2.removeEventListener(MouseEvent.CLICK, this.equipStrength);
			}
			this.btnback.removeEventListener(MouseEvent.CLICK, this.back);
			this.strengthbtn.removeEventListener(MouseEvent.CLICK, this.strMethod);
			this.mixturebtn.removeEventListener(MouseEvent.CLICK, this.mixMethod);
			this.resolutionbtn.removeEventListener(MouseEvent.CLICK, this.resMethod);
			this.makingbtn.removeEventListener(MouseEvent.CLICK, this.makeMethod);
			this.prePage.removeEventListener(MouseEvent.CLICK, this.prePageClick);
			this.nextPage.removeEventListener(MouseEvent.CLICK, this.nextPageClick);
			this.btnState = null;
		}
		
		private function prePageClick(param1:MouseEvent):void
		{
			if (this.backpackagemc)
			{
				this.backpackagemc.curPage -= 1;
			}
			this.nowpage.text = this.backpackagemc.curPage.toString();
		}
		
		private function nextPageClick(param1:MouseEvent):void
		{
			if (this.backpackagemc)
			{
				this.backpackagemc.curPage += 1;
			}
			this.nowpage.text = this.backpackagemc.curPage.toString();
		}
		
		private function equipStrength(param1:MouseEvent):void
		{
			if (this.player1)
			{
				if (this.player1.currentFrame == 2)
				{
					this.player1.gotoAndStop(1);
				}
			}
			else
			{
				trace("player1 not found!");
			}
			if (this.player2)
			{
				if (this.player2.currentFrame == 2)
				{
					this.player2.gotoAndStop(1);
				}
			}
			else
			{
				trace("player2 not found!");
			}
			param1.currentTarget.gotoAndStop(2);
			this.currentPlayer = this.onWho(String(param1.currentTarget.name));
			this.addBackPackAgeMc();
			if (this.currentPlayer)
			{
				this.strengthbtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			}
			this.setTxtlh();
		}
		
		private function strMethod(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			this.removeEquipCont();
			if (this.currentPlayer != null)
			{
				_loc2_ = new Strength(this.currentPlayer);
				_loc2_.name = "playerControl";
				_loc2_.x = 175.6;
				_loc2_.y = 128.45;
				this.addChild(_loc2_);
			}
			else
			{
				trace("currentPlayer is null");
			}
			if (this.btnState)
			{
				if (this.lastBtn != "")
				{
					this[this.lastBtn].upState = this.btnState;
				}
			}
			this.lastBtn = "strengthbtn";
			this.btnState = param1.target.upState;
			param1.target.upState = param1.target.downState;
		}
		
		private function mixMethod(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			this.removeEquipCont();
			if (this.currentPlayer != null)
			{
				_loc2_ = new Fusion(this.currentPlayer);
				_loc2_.name = "playerControl";
				_loc2_.x = 175.6;
				_loc2_.y = 128.45;
				this.addChild(_loc2_);
			}
			else
			{
				trace("currentPlayer is null");
			}
			if (this.btnState)
			{
				if (this.lastBtn != "")
				{
					this[this.lastBtn].upState = this.btnState;
				}
			}
			this.lastBtn = "mixturebtn";
			this.btnState = param1.target.upState;
			param1.target.upState = param1.target.downState;
		}
		
		private function resMethod(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			this.removeEquipCont();
			if (this.currentPlayer != null)
			{
				_loc2_ = new Resolution(this.currentPlayer);
				_loc2_.name = "playerControl";
				_loc2_.x = 175.6;
				_loc2_.y = 128.45;
				this.addChild(_loc2_);
			}
			else
			{
				trace("currentPlayer is null");
			}
			if (this.btnState)
			{
				if (this.lastBtn != "")
				{
					this[this.lastBtn].upState = this.btnState;
				}
			}
			this.lastBtn = "resolutionbtn";
			this.btnState = param1.target.upState;
			param1.target.upState = param1.target.downState;
		}
		
		private function makeMethod(param1:MouseEvent):void
		{
			var _loc2_:* = null;
			this.removeEquipCont();
			if (this.currentPlayer != null)
			{
				_loc2_ = new Making(this.currentPlayer);
				_loc2_.name = "playerControl";
				_loc2_.x = 175.6;
				_loc2_.y = 110.45;
				this.addChild(_loc2_);
			}
			else
			{
				trace("currentPlayer is null");
			}
			if (this.btnState)
			{
				if (this.lastBtn != "")
				{
					this[this.lastBtn].upState = this.btnState;
				}
			}
			this.lastBtn = "makingbtn";
			this.btnState = param1.target.upState;
			param1.target.upState = param1.target.downState;
		}
	}
}
